package com.long1008.controller.salary;

import cn.hutool.core.collection.CollUtil;

import com.long1008.pojo.EmpEc;
import com.long1008.pojo.Employee;
import com.long1008.pojo.SalaryMan;
import com.long1008.pojo.SalaryManExcel;
import com.long1008.pojo.*;
import com.long1008.common.poi.PoiUtils;
import com.long1008.service.EmpService;
import com.long1008.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 考勤记录
 *
 */
@RestController
@RequestMapping("/salary/table")
public class SalaryTableController {

    @Autowired
    SalaryService salaryService;
    @Autowired
    EmpService empService;

    Map<String, List<EmpEc>> resultOvertime = new HashMap<>();
    Map<String, List<EmpEc>> resultLeave = new HashMap<>();

    @RequestMapping("/man")
    public Map<String, Object> getEmployeeByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> map = new HashMap<>();
        List<Employee> employeeByPage = empService.getEmployeeByPageShort(page, size);
        Long count = empService.getCountByKeywords("", null, null, null, null, null, null, null);

        List<SalaryMan> salaryManList = new LinkedList<>();
        for (Employee employee : employeeByPage) {

            SalaryMan salaryMan = new SalaryMan();
            salaryMan.setEid(employee.getId());
            salaryMan.setName(employee.getName());
            salaryMan.setWorkID(employee.getWorkID());
            salaryMan.setLateCount(salaryService.getLateCount(employee.getId()));
            salaryMan.setLeaveCount(salaryService.getLeaveCount(employee.getId()));
            salaryMan.setOvertime(salaryService.getOvertime(employee.getId()));
            salaryMan.setRemark("该员工全部数据");
            salaryManList.add(salaryMan);
        }
        map.put("salaries", salaryManList);
        map.put("count", count);
        return map;
    }

    // 导出考勤记录
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ResponseEntity<byte[]> exportEmployeeSalary() {
        List<SalaryManExcel> salaryManExcels = new LinkedList<>();
        List<Employee> employees = empService.getAllEmployees();
        SalaryManExcel salaryManExcel = null;
        // 查询出来所有的员工id
        List<Long> empIdList = employees.stream().map(Employee::getId).collect(Collectors.toList());
        Map<String, Map<Long, Integer>> overtimeAndLateCountByEids = salaryService
                .getOvertimeAndLateCountByEids(empIdList);
        Map<Long, Integer> late = overtimeAndLateCountByEids.get("late");
        Map<Long, Integer> leave = overtimeAndLateCountByEids.get("leave");
        Map<Long, Integer> overtime = overtimeAndLateCountByEids.get("overtime");
        for (Employee employee : employees) {
            salaryManExcel = new SalaryManExcel();
            // 通过员工eid得到每个员工
            Long id = employee.getId();
            salaryManExcel.setEid(id);
            salaryManExcel.setWorkID(employee.getWorkID());
            salaryManExcel.setName(employee.getName());
            int overtimeByMonth = 0;
            int lateCountByMonth = 0;
            int leaveCountByMonth = 0;
            if (CollUtil.isNotEmpty(overtime)) {
                overtimeByMonth = overtime.get(id) == null ? 0 : overtime.get(id);
            }
            if (CollUtil.isNotEmpty(late)) {
                lateCountByMonth = late.get(id) == null ? 0 : late.get(id);
            }
            if (CollUtil.isNotEmpty(leave)) {
                leaveCountByMonth = leave.get(id) == null ? 0 : leave.get(id);
            }
            Date date = new Date();
            salaryManExcel.setLateCount(lateCountByMonth);
            salaryManExcel.setLeaveCount(leaveCountByMonth);
            salaryManExcel.setOvertime(overtimeByMonth);
            salaryManExcel.setRemark(date);
            salaryManExcels.add(salaryManExcel);
        }
        return PoiUtils.exportTableExcel(salaryManExcels);
    }

    // 迟到详情
    @GetMapping("/man/late")
    public Map<String, List<EmpEc>> getLateEmpEcByEidAndMonth(
            @RequestParam(required = false) Date month) {
        Map<String, List<EmpEc>> resultLate = new HashMap<>();
        resultOvertime = new HashMap<>();
        resultLeave = new HashMap<>();
        List<Employee> employees = empService.getAllEmployees();
        // 查询出来所有的员工id
        List<Long> empIdList = employees.stream().map(Employee::getId).collect(Collectors.toList());
        Map<String, Map<Long, List<EmpEc>>> overtimeAndLateAndLeaveCountByEids = salaryService
                .getOvertimeAndLateAndLeaveByEids(month, empIdList);
        Map<Long, List<EmpEc>> late = overtimeAndLateAndLeaveCountByEids.get("late");
        Map<Long, List<EmpEc>> overtime = overtimeAndLateAndLeaveCountByEids.get("overtime");
        Map<Long, List<EmpEc>> leave = overtimeAndLateAndLeaveCountByEids.get("leave");
        for (Employee employee : employees) {
            // 通过员工eid得到每个员工
            Long id = employee.getId();
            List<EmpEc> lateByMonth = new ArrayList<>();
            List<EmpEc> overtimeByMonth = new ArrayList<>();
            List<EmpEc> leaveByMonth = new ArrayList<>();
            if (CollUtil.isNotEmpty(late)) {
                lateByMonth = late.get(id) == null ? new ArrayList<>() : late.get(id);
                for (EmpEc lateByMonthSingle : lateByMonth) {
                    lateByMonthSingle.setEmpName(employee.getName());
                }
            }
            if (CollUtil.isNotEmpty(overtime)) {
                overtimeByMonth = overtime.get(id) == null ? new ArrayList<>() : overtime.get(id);
                for (EmpEc overtimeByMonthSingle : overtimeByMonth) {
                    overtimeByMonthSingle.setEmpName(employee.getName());
                }
            }
            if (CollUtil.isNotEmpty(leave)) {
                leaveByMonth = leave.get(id) == null ? new ArrayList<>() : leave.get(id);
                for (EmpEc leaveByMonthSingle : leaveByMonth) {
                    leaveByMonthSingle.setEmpName(employee.getName());
                }
            }
            resultLate.put(employee.getId().toString(), lateByMonth);
            resultOvertime.put(employee.getId().toString(), overtimeByMonth);
            resultLeave.put(employee.getId().toString(), leaveByMonth);
        }
        return resultLate;
    }

    // 加班详情
    @GetMapping("/man/overtime")
    public Map<String, List<EmpEc>> getOvertimeEmpEcByEidAndMonth() {
        return resultOvertime;
    }

    @GetMapping("/man/leave")
    public Map<String, List<EmpEc>> getLeaveEmpEcByEidAndMonth() {
        return resultLeave;
    }

    /**
     * 根据日期（月份）筛选考勤记录
     */
    @RequestMapping("/man/month/{month}")
    public Map<String, Object> getEmployeeByPageAndMonth(
            @PathVariable(required = false) Date month,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> map = new HashMap<>();
        List<Employee> employeeByPage = empService.getEmployeeByPageShort(page, size);
        Long count = empService.getCountByKeywords("", null, null, null, null, null, null, null);

        List<SalaryMan> salaryManList = new LinkedList<>();
        for (Employee employee : employeeByPage) {

            SalaryMan salaryMan = new SalaryMan();
            salaryMan.setEid(employee.getId());
            salaryMan.setName(employee.getName());
            salaryMan.setWorkID(employee.getWorkID());
            salaryMan.setLateCount(salaryService.getLateCountByMonth(employee.getId(), month));
            salaryMan.setLeaveCount(salaryService.getLeaveCountByMonth(employee.getId(), month));
            salaryMan.setOvertime(salaryService.getOvertimeByMonth(employee.getId(), month));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM", java.util.Locale.CHINA);
            String str = sdf.format(month);
            salaryMan.setRemark(str);
            salaryManList.add(salaryMan);
        }
        map.put("salaries", salaryManList);
        map.put("count", count);
        return map;
    }
}
